package com.kit.data.kitopeso.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.LayoutInflater;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;

import com.kit.data.kitopeso.KitoApp;
import com.kit.data.kitopeso.R;
import com.kit.data.kitopeso.base.BaseActivity;
import com.kit.data.kitopeso.databinding.KitoCountdownActivityBinding;
import com.kit.data.kitopeso.net.PU;
import com.kit.data.kitopeso.tools.LogUtil;
import com.kit.data.kitopeso.tools.ResUtils;
import com.kit.data.kitopeso.tools.ViewUtils;

public class KitoCountdownActivity extends BaseActivity<KitoCountdownActivityBinding> {


    private final int TIME_WHAT = 1;

    private int timeCout = 15;

    private boolean isBeforeApplyRisk;


    @Override
    protected KitoCountdownActivityBinding initViewBinding(LayoutInflater inflater) {
        return KitoCountdownActivityBinding.inflate(inflater);
    }

    @Override
    protected void initView() {
        binding.tvCs.setOnClickListener(v -> {
            ViewUtils.viewScale(v);
            startActivity(new Intent(getActivity(), KitoCustomerServiceActivity.class));
        });
        String str = timeCout + " S";
        binding.tvNum1.setText(str);
    }

    @Override
    protected void initData() {
        UP_TYPE1 = PU.P104;
        UP_TYPE2 = PU.P105;
    }

    @Override
    protected void onResume() {
        super.onResume();
        mHandler.sendEmptyMessageDelayed(TIME_WHAT, 1000);
        userProcess(new UserProcessInterface() {
            @Override
            public void onRequestResult() {
                boolean isCheckSteps = checkSteps();
                LogUtil.e("me", "isCheckSteps->" + isCheckSteps);
                if (isCheckSteps) {
                    mHandler.removeMessages(TIME_WHAT);
                }
            }
        }, true);
    }

    @SuppressLint("HandlerLeak")
    final
    Handler mHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == TIME_WHAT) {
                mHandler.removeMessages(TIME_WHAT);
                timeCout = timeCout - 1;
                if (timeCout == 10) {
                    binding.tvTxt1.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.countdown_box2, 0, 0);
                    binding.tvTxt2.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.countdown_box1, 0, 0);
                    binding.tvTxt3.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.countdown_box1, 0, 0);
                    FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT);
                    layoutParams.topMargin = ResUtils.d(R.dimen.dp_16);
                    binding.tvLineLayout.setLayoutParams(layoutParams);
                } else if (timeCout == 5) {
                    binding.tvTxt1.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.countdown_box2, 0, 0);
                    binding.tvTxt2.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.countdown_box2, 0, 0);
                    binding.tvTxt3.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.countdown_box1, 0, 0);
                    FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT);
                    layoutParams.topMargin = ResUtils.d(R.dimen.dp_16);
                    binding.tvLineLayout.setLayoutParams(layoutParams);
                } else if (timeCout == 0) {
                    binding.tvTxt1.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.countdown_box2, 0, 0);
                    binding.tvTxt2.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.countdown_box2, 0, 0);
                    binding.tvTxt3.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.countdown_box2, 0, 0);
                    FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT);
                    layoutParams.topMargin = ResUtils.d(R.dimen.dp_16);
                    binding.tvLineLayout.setLayoutParams(layoutParams);
                }
                if (timeCout <= 0) {
                    userProcess(() -> {
                        boolean isCheckSteps = checkSteps(getActivity());
                        if (!isCheckSteps) {
                            KitoApp.getInstance().clearActivity();
                            Intent intent = new Intent(getActivity(), KitoHomeActivity.class);
                            startActivity(intent);
                        }
                        finish();
                    }, true);

                } else {
                    String str = timeCout + " S";
                    binding.tvNum1.setText(str);
                    mHandler.sendEmptyMessageDelayed(TIME_WHAT, 1000);
                }
            }
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mHandler != null) {
            mHandler.removeMessages(TIME_WHAT);
        }
    }

}
